package 刷题.leedcode232;
//用两个栈实现队列
import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

public class MyQueue {
    Deque<Integer> stack1;
    Deque<Integer> stack2;
    public MyQueue() {
        stack1 = new LinkedList<Integer>();
        stack2 = new LinkedList<Integer>();
    }

    public void push(int x) {//将x放在队尾，先出
        stack1.push(x);
    }

    public int pop() {//返回队首元素并删除,最底下的元素
        if(stack2.isEmpty()){
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }

    public int peek() {//返回队首元素不删除
        if(stack2.isEmpty()){
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.peek();
    }

    public boolean empty() {
        return stack1.isEmpty()&& stack2.isEmpty();
    }
}
